﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Silverlight.Navigation.History;
using Silverlight.Navigation.Mapping;

namespace Silverlight.Navigation.Controls
{
    public partial class History : ChildWindow
    {
        #region Events

        public event EventHandler ClearHistory;

        #endregion

        #region Fields

        private HistoryManager _historyManager;
        private string _selectedUri = string.Empty;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the History class.
        /// </summary>
        public History()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Initializes a new instance of the History class.
        /// </summary>
        /// <param name="historyManager"></param>
        public History(HistoryManager historyManager)
            : this()
        {
            if (historyManager == null)
                throw new ArgumentNullException("historyManager");

            this.HistoryManager = historyManager;
        }

        #endregion

        #region Properties

        /// <summary>
        /// This history manager to use.
        /// </summary>
        public HistoryManager HistoryManager
        {
            get { return _historyManager; }
            set { _historyManager = value; }
        }

        /// <summary>
        /// The selected uri.
        /// </summary>
        public string SelectedUri 
        {
            get { return _selectedUri; }
            private set { _selectedUri = value; }
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Handles the loaded event for the control.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HistoryControl_Loaded(object sender, RoutedEventArgs e)
        {
            LoadHistory();
        }

        /// <summary>
        /// Handles the click event of the clear button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ClearButton_Click(object sender, RoutedEventArgs e)
        {
            this.HistoryManager.Clear();

            if (ClearHistory != null)
                ClearHistory(this, new EventArgs());

            this.DialogResult = false;
            this.Close();
        }

        /// <summary>
        /// Handles the click event of the hyperlink button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            if (sender is HyperlinkButton)
            {
                HyperlinkButton hyperlinkButton = (HyperlinkButton)sender;

                if (hyperlinkButton != null && hyperlinkButton.DataContext is HistoryTreeViewItem)
                {
                    HistoryTreeViewItem historyTreeViewItem = (HistoryTreeViewItem)hyperlinkButton.DataContext;

                    this.SelectedUri = historyTreeViewItem.Title;
                    this.DialogResult = true;
                    this.Close();
                }
                
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Loads the history.
        /// </summary>
        private void LoadHistory()
        {
            if (_historyManager != null)
            {
                HistoryTreeViewItemCollection historyTreeViewItems = new HistoryTreeViewItemCollection();

                historyTreeViewItems.Add(new HistoryTreeViewItem("Today", _historyManager.HistoryItems.Today()));
                historyTreeViewItems.Add(new HistoryTreeViewItem("This Week", _historyManager.HistoryItems.ThisWeek()));
                historyTreeViewItems.Add(new HistoryTreeViewItem("Last Week", _historyManager.HistoryItems.LastWeek()));
                historyTreeViewItems.Add(new HistoryTreeViewItem("Two Weeks Ago", _historyManager.HistoryItems.TwoWeeksAgo()));
                historyTreeViewItems.Add(new HistoryTreeViewItem("More Than Two Weeks Ago", _historyManager.HistoryItems.MoreThanTwoWeeksAgo()));

                this.HistoryItems.ItemsSource = historyTreeViewItems;
            }
        }

        #endregion
    }

    /// <summary>
    /// Represents a history tree view item.
    /// </summary>
    public class HistoryTreeViewItem
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instnace of the HistoryTreeViewItem class.
        /// </summary>
        public HistoryTreeViewItem() { }

        /// <summary>
        /// Initializes a new instance of the HistoryTreeViewItem class.
        /// </summary>
        /// <param name="title"></param>
        /// <param name="historyItems"></param>
        public HistoryTreeViewItem(string title, HistoryItemCollection historyItems)
        {
            this.Title = title;
            this.IsLink = false;

            if (historyItems != null)
            {
                this.HistoryItems = new HistoryTreeViewItemCollection();

                foreach (HistoryItem historyItem in historyItems)
                {
                    this.HistoryItems.Add(new HistoryTreeViewItem { Title = historyItem.Uri, IsLink = true });
                }
            }
        }

        #endregion

        #region Properties

        /// <summary>
        /// Whether or not the item is a link.
        /// </summary>
        public bool IsLink { get; set; }

        /// <summary>
        /// Title of the tree view item.
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// Items in the tree view item.
        /// </summary>
        public HistoryTreeViewItemCollection HistoryItems { get; set; }

        #endregion

        #region Methods

        /// <summary>
        /// Returns a string representing the object.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return this.Title;
        }

        #endregion
    }

    /// <summary>
    /// Represents a collection of history tree view items.
    /// </summary>
    public class HistoryTreeViewItemCollection : ObservableCollection<HistoryTreeViewItem>
    {

    }
}

